package xyz.xtt.schedule.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import xyz.xtt.schedule.entity.ChainTransactionEntity;
import xyz.xtt.schedule.mapper.blockchain.ChainTransactionMapper;

@Service
public class ChainTransactionService {
	@Autowired
	ChainTransactionMapper chainTransactionMapper;

	public boolean addTransaction(ChainTransactionEntity transaction) {
		if (transaction != null) {
			// 查找数据库里是否存在该hash数据，再插入
			QueryWrapper<ChainTransactionEntity> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("hash", transaction.getHash());
			ChainTransactionEntity existing = chainTransactionMapper.selectOne(queryWrapper);
			if (existing == null) {
				int result = chainTransactionMapper.insert(transaction);
				return result > 0;
			}
		}
		return false;
	}
}
